package top.wkq.middleware.dynamic.thread.pool.sdk.trigger.job;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import top.wkq.middleware.dynamic.thread.pool.sdk.domain.DynamicThreadPoolService;
import top.wkq.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import top.wkq.middleware.dynamic.thread.pool.sdk.registry.IRegistry;

import java.util.List;

/**
 * <h1>线程池上报任务</h1>
 */
public class ThreadPoolDataReportJob {
    private Logger logger = LoggerFactory .getLogger(getClass());
    private final IRegistry registry;
    private final DynamicThreadPoolService dynamicThreadPoolService;

    public ThreadPoolDataReportJob(IRegistry registry, DynamicThreadPoolService dynamicThreadPoolService) {
        this.registry = registry;
        this.dynamicThreadPoolService = dynamicThreadPoolService;
    }

    @Scheduled(cron = "0/20 * * * * ?")
    public void executeThreadPoolDataReportJob()
    {
      // 查询线程池配置列表
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.getThreadPoolConfigList();
        registry.reportThreadPool(threadPoolConfigEntities);
        logger.info("动态线程池， 上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));
        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
            registry.reportThreadPoolParameter(threadPoolConfigEntity);
            logger.info("动态线程池， 上报线程池参数信息：{}", JSON.toJSONString(threadPoolConfigEntity));
        }
    }
}
